#include "hardinterface.h"
#include <iostream>

static const quint16 simPort = 44000;

HardInterface::HardInterface(int geschwSoll, int spurSoll)
{
    count = 0;
    this->commCtl = new sendReceiveControll();
    this->timer = new QTimer();
    this->geschwSoll = geschwSoll;
    this->spurSoll = spurSoll;

    initHardware();

}

//destruktor
HardInterface::~HardInterface()
{
    delete commCtl;
    delete timer;
}

bool HardInterface::initHardware()
{
    connect(timer, SIGNAL(timeout()), this, SLOT(tryToInit()));
    connect(commCtl,SIGNAL(dataReadyRead(QHostAddress,quint16,QByteArray)),
            this, SLOT(initOK(QHostAddress,quint16,QByteArray)));
    timer->start(1000);
    return true;
}

void HardInterface::tryToInit()
{
    if(count < 30)
    {
        QByteArray start("newcar");
        commCtl->sendBroadcast(start);
        std::cout << "Sende Broadcast" << std::endl;
        std::cout << "Keine Simulation gestartet !" << std::endl;
        count++;
    }
    else
    {
       std::cout << "Keine Simulation gestartet !" << std::endl;
       timer->stop();
    }
}

void HardInterface::initOK(QHostAddress senderIP, quint16 senderPort,QByteArray data)
{
    timer->stop();
    this->simIP = senderIP;
    QByteArray myDat("newcar");

    if(data == myDat){
        std::cout << "Eigene Packete empfangen." << std::endl;
    }

    QList<QByteArray> list = data.split('@');
    if (list.size() != 3)
    {
        std::cout << "FEHLER bei Broadcast Ubertragung!" << std::endl;
        timer->start(1000);
        return;
    }
    carId = list.at(0).toInt();
    int spur = list.at(1).toInt();
    int weite = list.at(2).toInt();

    //erfogleich init, also disconnecte
    disconnect(commCtl,SIGNAL(dataReadyRead(QHostAddress,quint16,QByteArray)),
            this, SLOT(initOK(QHostAddress,quint16,QByteArray)));
    disconnect(timer, SIGNAL(timeout()), this, SLOT(tryToInit()));
    //verbinde zu datenaustausch
    connect(commCtl, SIGNAL(dataReadyRead(QHostAddress,quint16,QByteArray)),
            this, SLOT(simDataReceived(QHostAddress,quint16,QByteArray)));

    std::cout << "Sim Antwort." << std::endl;

    QByteArray ok("OK");
    commCtl->sendData(simIP, simPort, ok);

    emit initReady(carId, spur, weite);


}

void HardInterface::sendDataToSim(QByteArray carData)
{
    QByteArray temp;
    temp.setNum(this->geschwSoll);
    carData.append(temp);
    temp.clear();
    temp.setNum(this->spurSoll);
    carData.append("@");
    carData.append(temp);

    commCtl->sendData(simIP, simPort, carData);
}


void HardInterface::simDataReceived(QHostAddress senderIP, quint16 senderPort,QByteArray data)
{
    int count = 0;
    std::cout << "Sim Antwort222." << std::endl;

    QByteArray sim("Sim");
    QList<QByteArray> listDaten = data.split('!');
    QList<QByteArray> listAutoDat;

    if(listDaten.size() != 0 && listDaten.at(0) == sim)
    {
        //suche daten fuer dieses auto
        listDaten.removeAt(0);
        foreach(QByteArray carDat, listDaten)
        {
            listAutoDat = carDat.split('@');
            if(listAutoDat.at(0).toInt() == carId)
            {
                listDaten.removeAt(count);
               // emit aktualisiereCarsList(listDaten);
                emit aktualisiereCarData(carDat, listDaten);
                break;
            }
            count++;
        }     
    }



}

